package com.platform.task.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.platform.common.cache.BaseTableCache;
import com.platform.common.cache.CacheManager;
import com.platform.common.util.DateUtils;
import com.platform.task.dao.BaseCyberMapper;
import com.platform.task.entity.Base_Cyber;
import com.platform.task.service.BaseCyberService;

/**
 * 数据入库
 * 
 * @desc
 * @author wangsh
 * @date Oct 18, 2017:10:55:49 AM
 */
@Service
public class BaseCyberServiceImpl implements BaseCyberService {
	private static final Logger log = Logger.getLogger(BaseCyberServiceImpl.class);

	@Autowired
	private BaseCyberMapper baseCyberMapper;
	@Autowired
	private CacheManager cacheManager;

	/**
	 * 定时执行数据入库，并处理重复数据，如果数据存在，则更新；如果数据不错在，则插入
	 */
	@Override
	public void addBaseCyberBatch(List<Base_Cyber> baseCybers) {
		try {
			// 缓存中的IMSI集合
			Set<Object> cache = BaseTableCache.cache.get("[WFBDCMain].[dbo].[EFMS_tb_Cyber]");
			if (cache == null) {
				cache = new HashSet<Object>();
			}
			Map<String, Object> unitGuild = cacheManager.getCache("WFBDCMain_dbo_WFBDC_tb_Unit", "cacheUnitGuildList");
			// 最终插入到SqlServer的集合
			Set<Base_Cyber> finalUpdateList = new HashSet<Base_Cyber>();
			Set<Base_Cyber> finalInsertList = new HashSet<Base_Cyber>();
			Set<String> newKeys = new HashSet<String>();
			for (Base_Cyber baseCyber : baseCybers) {
				try {
					String unitCode = baseCyber.getUnitCode();
					String cyberID = baseCyber.getProtoType() + "_" + baseCyber.getCyberCode();

					// 处理时间
					dealWithTime(baseCyber);

					baseCyber.setGuildID(unitGuild.get(unitCode).toString());
					baseCyber.setCyberID(cyberID);
					// 对比找出新增的集合在缓存中是否存在,如果存在则不插入,否则获取第一个进行更新操作
					if (cache.contains(cyberID)) {
						// 如果缓存中已经存在,则判断是否需要更新
						finalUpdateList.add(baseCyber);
					} else {
						// 如果更新队列中不存在,则更新
						if (!newKeys.contains(cyberID)) {
							finalInsertList.add(baseCyber);
							newKeys.add(cyberID);
						}
					}
				} catch (Exception e) {
					continue;
				}
			}
			baseCyberMapper.batchInsert(finalInsertList);
			baseCyberMapper.batchUpdate(finalUpdateList);
			// TODO 将新增的数据更新到缓存里面
			cache.addAll(newKeys);
			BaseTableCache.cache.put("[WFBDCMain].[dbo].[EFMS_tb_Cyber]", cache);
		} catch (Exception e) {
			log.error("BaseCyber数据入库发生一个异常,传入的参数为:" + baseCybers + "具体异常:", e);
		}
	}

	/**
	 * @author zhanghan
	 * @Description: TODO 处理时间为空的情况
	 * @param baseCyber
	 * @return void
	 * @date 2017年9月20日
	 */
	private void dealWithTime(Base_Cyber baseCyber) {
		String time = StringUtils.isEmpty(baseCyber.getTime()) ? DateUtils.currentDatetime() : baseCyber.getTime();
		baseCyber.setTime(time);
		String writerTime = StringUtils.isEmpty(baseCyber.getWriterTime()) ? DateUtils.currentDatetime() : baseCyber
				.getWriterTime();
		baseCyber.setWriterTime(writerTime);
		String createTime = StringUtils.isEmpty(baseCyber.getCreateTime()) ? DateUtils.currentDatetime() : baseCyber
				.getCreateTime();
		baseCyber.setCreateTime(createTime);
	}

}
